package com.devops.admin.util;

import cn.hutool.core.util.StrUtil;
import org.springframework.format.annotation.DateTimeFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @ClassName TimeUtil
 * @Description TODO
 * @Author hepingguo
 * @Date 2022/4/27 15:55
 * @Version 1.0
 **/
public class TimeUtil {

    private static final String localTimePatten = "yyyy-MM-dd HH:mm:ss";

    public static LocalDateTime utcStrToLocalDateTime(String date) {
        if (StrUtil.isBlank(date)) {
            return null;
        }
        String localDate = getDate(date);
        return LocalDateTime.parse(localDate,DateTimeFormatter.ofPattern(localTimePatten));
    }

    public static String getDate(String date) {
        String result;
        if (StrUtil.isNotBlank(date)) {
            String utcTimePatten = "yyyy-MM-dd'T'HH:mm:ss'Z'";
            result = utc2Local(date,utcTimePatten,localTimePatten);
        } else {
            result = "";
        }
        return result;
    }

    public static String utc2Local(String utcTime, String utcTimePatten, String localTimePatten) {
        SimpleDateFormat utcFormat = new SimpleDateFormat(utcTimePatten);
        utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));//时区定义并进行时间获取
        Date gpsUTCDate = null;
        try {
            gpsUTCDate = utcFormat.parse(utcTime);
        } catch (ParseException e) {
            e.printStackTrace();
            return utcTime;
        }
        SimpleDateFormat localFormat = new SimpleDateFormat(localTimePatten);
        localFormat.setTimeZone(TimeZone.getDefault());
        return localFormat.format(gpsUTCDate.getTime());
    }

    public static int getDatePoor(Date startDate, Date endDate){
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        Long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        Long mymint = diff/1000/60;
        int intValue = mymint.intValue();
        return intValue;
    }

    public static LocalDateTime getDateEnd(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        LocalDateTime dateTime = LocalDateTime.parse(format.format(calendar.getTime()),df);

        return dateTime;
    }

}
